package db;
import java.sql.*;
import java.util.*;
import beans.*;


public class ksDAO extends ConnBean{
	private Connection connection=null;
	public List<ksBean> getAllRecords(){
		ResultSet rs=null;
		Statement stmt=null;
		ArrayList <ksBean> list=new ArrayList<ksBean>();
		try {	
			connection = getConnection();
			if(connection==null)
				System.out.println("error");
			String sql="select * from YY_ks";
			stmt =connection.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				ksBean ks=new ksBean();
				ks.setId(rs.getString(1));
				ks.setName(rs.getString(2));
				list.add(ks);
			}
			stmt.close();						
			connection.close();			
			
	}catch (SQLException e) {
		e.printStackTrace();
	} finally{
		try{
			if (connection!=null && (!connection.isClosed())){
				connection.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
		return list;
	}
	
	public ksBean getRecord(String ksid){
		ResultSet rs=null;
		Statement stmt=null;
		ksBean ks=null;
		try {			
			connection = getConnection();
			if(connection==null)
				System.out.println("error");
			String sql="select * from YY_ks where id='"+ksid+"'";
			stmt =connection.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs!=null && rs.next()){
				System.out.println("right");
				ks=new ksBean();
				ks.setId(rs.getString(1));
				ks.setName(rs.getString(2));
			}
			else
			{	
				System.out.println("null");
				ks=null;
			}
			stmt.close();					
			connection.close();	
		}catch (SQLException e) {
			e.printStackTrace();
		} finally{
		try{
			if (connection!=null && (!connection.isClosed())){
				connection.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
		return ks;
  }	
	public ksBean getRecord1(String name){
		ResultSet rs=null;
		Statement stmt=null;
		ksBean ks=null;
		try {			
			connection = getConnection();
			if(connection==null)
				System.out.println("error");
			String sql="select * from YY_ks where name='"+name+"'";
			stmt =connection.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs!=null && rs.next()){
				System.out.println("right");
				ks=new ksBean();
				ks.setId(rs.getString(1));
				ks.setName(rs.getString(2));
			}
			else
			{	
				System.out.println("null");
				ks=null;
			}
			stmt.close();					
			connection.close();	
		}catch (SQLException e) {
			e.printStackTrace();
		} finally{
		try{
			if (connection!=null && (!connection.isClosed())){
				connection.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
		return ks;
  }	
	public boolean insertRecord(ksBean record)
	{
		PreparedStatement pstmt=null;
		String sql="insert into YY_ks values(?,?)";
		if(record==null) 
			return false;
		boolean result=false;
		int number=0;
		try{
			connection=getConnection();
			pstmt=connection.prepareStatement(sql);
			pstmt.setString(1, record.getId());
			pstmt.setString(2, record.getName());
			number=pstmt.executeUpdate();
			pstmt.close();						
			connection.close();	
		}catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try{
				if (connection!=null && (!connection.isClosed())){
					connection.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(number>0)
			result=true;
		return result;
	}

	public boolean deleteRecord(ksBean record)
	{
		PreparedStatement pstmt=null;
		String sql="delete from YY_ks where id=?";
		
		if(record==null) 
			return false;
		int result=0;
		try{
			connection=getConnection();
			pstmt=connection.prepareStatement(sql);
			pstmt.setString(1, record.getId());
			result=pstmt.executeUpdate();
			System.out.println(record.getId());
			pstmt.close();						
			connection.close();	
		}catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try{
				if (connection!=null && (!connection.isClosed())){
					connection.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(result>0)
			return true;
		else
			return false;
	}
	
	public boolean updateRecord(ksBean beforeRecord,ksBean afterRecord)
	{
		if(beforeRecord==null || afterRecord==null) 
			return false;
		PreparedStatement pstmt=null;
		String sql="update YY_ks set id=?,name=?"+"where id="+beforeRecord.getId();
		int result=0;
		try{
			connection=getConnection();
			pstmt=connection.prepareStatement(sql);
			pstmt.setString(1, afterRecord.getId());
			pstmt.setString(2, afterRecord.getName());
			result=pstmt.executeUpdate();
			pstmt.close();					
			connection.close();	
		}catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try{
				if (connection!=null && (!connection.isClosed())){
					connection.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(result>0)
			return true;
		else
			return false;
	}
	
}
